package com.example.yian.service;

import com.example.yian.entity.TextSearchRequest;
import com.example.yian.entity.TextSearchResponse;
import com.example.yian.mapper.TextSearchRequestMapper;
import com.example.yian.mapper.TextSearchResponseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class TextSearchService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private TextSearchRequestMapper textSearchRequestMapper;

    @Autowired
    private TextSearchResponseMapper textSearchResponseMapper;

    public TextSearchRequest createRequest(String textMessage, Boolean reChat, Boolean stream, Long userId) {
        TextSearchRequest request = new TextSearchRequest();
        request.setTextMessage(textMessage);
        request.setReChat(reChat);
        request.setStream(stream);
        request.setUserId(userId);
        request.setCreatedAt(LocalDateTime.now());
        textSearchRequestMapper.insert(request);
        return request;
    }

    public TextSearchResponse createResponse(Long requestId, String chatId, String textId, Boolean operate,
                                             String content, String refText, Boolean isEnd) { // 修改这里
        TextSearchResponse response = new TextSearchResponse();
        response.setRequestId(requestId);
        response.setChatId(chatId);
        response.setTextId(textId);
        response.setOperate(operate);
        response.setContent(content);
        response.setRefText(refText); // 修改这里
        response.setIsEnd(isEnd);
        response.setCreatedAt(LocalDateTime.now());
        response.setUpdatedAt(LocalDateTime.now());
        textSearchResponseMapper.insert(response);
        return response;
    }

    public Optional<TextSearchResponse> findByTextId(String textId) {
        return Optional.ofNullable(textSearchResponseMapper.selectByTextId(textId));
    }


    public Map<String, Object> callThirdPartyApi(String chatId, String message, boolean reChat, boolean stream) {
        String url = "http://120.26.5.41:8080/api/application/chat_message/" + chatId;
        HttpHeaders headers = new HttpHeaders();
        headers.set("accept", "application/json");
        headers.set("Authorization", "application-8900dd45b50a0e7d2b06bef61c526df0");
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("message", message);
        requestBody.put("re_chat", reChat);
        requestBody.put("stream", stream);
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
        ResponseEntity<Map> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, Map.class);
        return response.getBody();
    }
}
